package cn.lyjuan.android.demofive.context;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.lyjuan.android.demofive.frame.realm.UserModel;
import cn.lyjuan.android.mylibrary.context.RealmApplication;
import io.realm.DynamicRealm;
import io.realm.RealmSchema;
import io.realm.annotations.RealmModule;

/**
 * Created by chad on 08/11/2017.
 */

public class MyApplication extends RealmApplication
{
    private static final String TAG = MyApplication.class.getSimpleName();

    private static final Integer NEW_VERSION = 6;

    @Override
    protected long realmVersion()
    {
        return NEW_VERSION;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    protected void init(Context context)
    {
        super.init(context);

        long version = realm().getVersion();

        Log.d(TAG, "realm curr version >> " + version);
    }


    @Override
    protected Object[] modules()
    {
        return new Object[]{new DemoFiveRealmModules()};
    }

    /**
     * 指定Realm需要加载当前模块
     */
    @RealmModule(library = true, allClasses = true)
    public class DemoFiveRealmModules
    {
    }

    @Override
    protected List<RealmApplication.SubMigration> subMigrations()
    {
        List<RealmApplication.SubMigration> list = new ArrayList<>();
        list.add(
                new RealmApplication.SubMigration(5)
                {
                    @Override
                    public void migrate(DynamicRealm realm)
                    {
                        RealmSchema schema = realm.getSchema();

                        createSechem(schema, UserModel.class);
                    }
                });
        return list;
    }
}
